package com.njtoyo.taxi.admin.rest.controller.platform;

import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.rest.wrapper.platform.resource.ResourceCreateWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.platform.resource.ResourceUpdateSortWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.platform.resource.ResourceUpdateWrapper;
import com.njtoyo.taxi.admin.service.platform.ResourceService;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/platform/resource")
@Api(tags = "资源权限")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("")
    @ApiOperation(value = "获取资源权限列表", notes = "权限：resource")
    @PermissionScope(names = {"resource", "role_create", "role_update"})
    public RestResult getList() {
        return resourceService.getList();
    }

    @PostMapping("")
    @ApiOperation(value = "新增资源权限", notes = "权限：resource_create")
    @PermissionScope(names = {"resource_create"})
    public RestResult createResource(@Valid @RequestBody ResourceCreateWrapper resourceCreateWrapper) {
        return resourceService.createResource(resourceCreateWrapper);
    }

    @PatchMapping("/{id}")
    @ApiOperation(value = "修改资源权限", notes = "权限：resource_update")
    @PermissionScope(names = {"resource_update"})
    public RestResult updateResource(@PathVariable("id") Long id, @RequestBody ResourceUpdateWrapper params) {
        return resourceService.updateResource(id, params);
    }

    @PatchMapping("/{parent_id}/sort")
    @ApiOperation(value = "修改资源权限排序", notes = "权限：resource_update")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "json", name = "params", value = "resourceIds: [] 资源ID集合，必填，按照新的排序顺序")})
    @PermissionScope(names = {"resource_update"})
    public RestResult updateResourceSort(@PathVariable("parent_id") Long parentId, @RequestBody ResourceUpdateSortWrapper params) {
        return resourceService.updateResourceSort(parentId, params);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除资源权限", notes = "权限：resource_delete")
    @PermissionScope(names = {"resource_update"})
    public RestResult deleteResource(@PathVariable("id") Long id) {
        return resourceService.deleteResource(id);
    }
}
